package com.android.qjpmc.application_module.viewModel;

import com.alibaba.fastjson.JSONObject;
import com.android.qjpmc.application_module.contract.RtDataContract;
import com.android.qjpmc.application_module.home.model.StationDataModel;
import com.android.qjpmc.application_module.model.RtDataModel;
import com.android.qjpmc.bean.GroupInfoPointDataBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.widget.mvvm.factory.CreateModel;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Function;

@CreateModel(RtDataModel.class)
public class RtDataViewModel extends RtDataContract.ViewModel {


    @Override
    public void getPointDataOfGroup(String stationId, PwrDeviceBean groupDataBean) {
        mModel.getPointListOfGroup(groupDataBean.groupId, 1)
                .subscribe(new ProgressObserver<List<PointParameterBean>>(false, this) {
                    @Override
                    public void _onNext(List<PointParameterBean> pointList) {

                        getPointDataOfStation(stationId, groupDataBean, pointList);

                    }
                });
    }

    private void getPointDataOfStation(String stationId, PwrDeviceBean groupDataBean, List<PointParameterBean> pointList) {
        List<String> pointCodeList = new ArrayList<>();
        for (PointParameterBean groupPointListBean : pointList) {
            pointCodeList.add(StationDataModel.buildPointCode(groupPointListBean));
        }
        mModel.getPointDataOfPointCode(stationId, pointCodeList).map(new Function<Object, List<PointParameterBean>>() {
            @Override
            public List<PointParameterBean> apply(Object o) throws Exception {
                String s = JSONObject.toJSONString(o);
                Logger.d("getPointDataOfStation" + s);
                JSONObject jsonObject = JSONObject.parseObject(s);
                Logger.d("getPointDataOfStation22" + Thread.currentThread().getName());
                for (PointParameterBean groupPointListBean : pointList) {
                   String pointCode = StationDataModel.buildPointCode(groupPointListBean);
                    Object pointData = jsonObject.get(pointCode);
                    GroupInfoPointDataBean groupInfoPointDataBean = JSONObject.parseObject(JSONObject.toJSONString(pointData), GroupInfoPointDataBean.class);
                    groupPointListBean.pointData = groupInfoPointDataBean;
                }
                return pointList;
            }
        })
                .subscribe(new ProgressObserver<List<PointParameterBean>>(false, this) {
                    @Override
                    public void _onNext(List<PointParameterBean> o) {
                        groupDataBean.isGetPointReamTimeData = true;
                        groupDataBean.pointList = pointList;
                        mView.returnGroupPointList(groupDataBean);
                    }
                });


    }
}
